/*!
 * \file test_object_pool_simple.cpp
 * \project WonderTrader
 * 
 * \brief ObjectPool对象池简化测试套件
 * 
 * \details 测试ObjectPool模板类的基本功能，避免复杂的并发和大量对象测试
 */

#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include "../../src/Share/ObjectPool.hpp"
#include <vector>

/*!
 * \brief 测试用的简单类
 */
class SimpleTestObject {
public:
    static int construction_count;
    static int destruction_count;
    
    int value;
    
    SimpleTestObject() : value(42) {
        construction_count++;
    }
    
    SimpleTestObject(int v) : value(v) {
        construction_count++;
    }
    
    ~SimpleTestObject() {
        destruction_count++;
    }
    
    static void reset_counters() {
        construction_count = 0;
        destruction_count = 0;
    }
};

int SimpleTestObject::construction_count = 0;
int SimpleTestObject::destruction_count = 0;

/*!
 * \brief ObjectPool简化测试套件
 */
class ObjectPoolSimpleTest : public ::testing::Test {
protected:
    void SetUp() override {
        SimpleTestObject::reset_counters();
    }
    
    void TearDown() override {
        // 测试后验证
    }
};

// ========== 基本功能测试 ==========

/*!
 * \brief 测试基本对象构造
 */
TEST_F(ObjectPoolSimpleTest, TestBasicConstruction) {
    ObjectPool<SimpleTestObject> pool;
    
    // 构造对象
    SimpleTestObject* obj = pool.construct();
    ASSERT_NE(obj, nullptr);
    EXPECT_EQ(obj->value, 42);
    EXPECT_EQ(SimpleTestObject::construction_count, 1);
    EXPECT_EQ(SimpleTestObject::destruction_count, 0);
    
    // 销毁对象
    pool.destroy(obj);
    EXPECT_EQ(SimpleTestObject::destruction_count, 1);
}

/*!
 * \brief 测试多个对象构造和销毁
 */
TEST_F(ObjectPoolSimpleTest, TestMultipleObjects) {
    ObjectPool<SimpleTestObject> pool;
    std::vector<SimpleTestObject*> objects;
    
    const int num_objects = 5; // 减少对象数量
    
    // 构造多个对象
    for (int i = 0; i < num_objects; i++) {
        SimpleTestObject* obj = pool.construct();
        ASSERT_NE(obj, nullptr);
        EXPECT_EQ(obj->value, 42);
        objects.push_back(obj);
    }
    
    EXPECT_EQ(SimpleTestObject::construction_count, num_objects);
    EXPECT_EQ(SimpleTestObject::destruction_count, 0);
    
    // 销毁所有对象
    for (SimpleTestObject* obj : objects) {
        pool.destroy(obj);
    }
    
    EXPECT_EQ(SimpleTestObject::destruction_count, num_objects);
}

/*!
 * \brief 测试内存重用
 */
TEST_F(ObjectPoolSimpleTest, TestMemoryReuse) {
    ObjectPool<SimpleTestObject> pool;
    
    // 第一轮：构造和销毁对象
    std::vector<SimpleTestObject*> first_batch;
    for (int i = 0; i < 3; i++) {
        first_batch.push_back(pool.construct());
    }
    
    // 记录第一批对象的地址
    std::vector<void*> first_addresses;
    for (SimpleTestObject* obj : first_batch) {
        first_addresses.push_back(obj);
        pool.destroy(obj);
    }
    
    // 第二轮：再次构造对象
    std::vector<SimpleTestObject*> second_batch;
    for (int i = 0; i < 3; i++) {
        second_batch.push_back(pool.construct());
    }
    
    // 验证内存重用（至少有一些地址应该重复）
    int reused_count = 0;
    for (SimpleTestObject* obj : second_batch) {
        for (void* addr : first_addresses) {
            if (obj == addr) {
                reused_count++;
                break;
            }
        }
        pool.destroy(obj);
    }
    
    // 应该有内存重用
    EXPECT_GT(reused_count, 0);
}

/*!
 * \brief 测试内存释放
 */
TEST_F(ObjectPoolSimpleTest, TestMemoryRelease) {
    ObjectPool<SimpleTestObject> pool;
    std::vector<SimpleTestObject*> objects;
    
    // 构造一些对象
    for (int i = 0; i < 10; i++) {
        objects.push_back(pool.construct());
    }
    
    // 销毁所有对象
    for (SimpleTestObject* obj : objects) {
        pool.destroy(obj);
    }
    
    // 手动释放内存
    pool.release();
    
    // 验证可以继续使用
    SimpleTestObject* new_obj = pool.construct();
    EXPECT_NE(new_obj, nullptr);
    pool.destroy(new_obj);
}

/*!
 * \brief 测试空对象池操作
 */
TEST_F(ObjectPoolSimpleTest, TestEmptyPoolOperations) {
    ObjectPool<SimpleTestObject> pool;
    
    // 在空池上调用release应该安全
    pool.release();
    
    // 构造对象应该仍然工作
    SimpleTestObject* obj = pool.construct();
    EXPECT_NE(obj, nullptr);
    pool.destroy(obj);
}

/*!
 * \brief 测试重复释放
 */
TEST_F(ObjectPoolSimpleTest, TestRepeatedRelease) {
    ObjectPool<SimpleTestObject> pool;
    
    // 构造和销毁一些对象
    SimpleTestObject* obj = pool.construct();
    pool.destroy(obj);
    
    // 多次调用release应该安全
    pool.release();
    pool.release();
    pool.release();
    
    // 之后仍应该能正常工作
    obj = pool.construct();
    EXPECT_NE(obj, nullptr);
    pool.destroy(obj);
} 